namespace CIS
{
    using CISLib;
    using CISLib.EncryptClasses;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class MainPage : Page
    {
        protected Image iAnnounce;
        protected Label lbAnnounce;
        protected Label lbCustomerBirth;
        protected Label lbCustomerFamilyMemberBirth;
        protected Label lbCustomerMarriage;
        protected Label lbUser;
        protected Label lbUserTitle;
        protected UserPrincipal Principal = null;
        protected Repeater RptAnnounce;
        protected Repeater rptCustomerBirth;
        protected Repeater rptCustomerFamilyMemberBirth;
        protected Repeater rptCustomerMarriage;

        private void DateBind()
        {
            ArrayList idAryList = new ArrayList();
            Area[] areaAry = this.Principal.OwnArea;
            Province[] provinceAry = this.Principal.Province;
            Cat[] catAry = this.Principal.OwnCat;
            for (int i = 0; i < areaAry.Length; i++)
            {
                for (int j = 0; j < catAry.Length; j++)
                {
                    for (int x = 0; x < provinceAry.Length; x++)
                    {
                        idAryList.AddRange(RemindCustomerInfo.GetCustomerIdAry(catAry[j].CatId, provinceAry[x].ProvinceId, areaAry[i].AreaId));
                    }
                }
            }
            Guid[] idAry = (Guid[]) idAryList.ToArray(typeof(Guid));
            this.rptCustomerBirth.DataSource = this.GetBirthData(idAry);
            this.rptCustomerBirth.DataBind();
            this.rptCustomerFamilyMemberBirth.DataSource = this.GetMBirthData(idAry);
            this.rptCustomerFamilyMemberBirth.DataBind();
            this.rptCustomerMarriage.DataSource = this.GetMarriageData(idAry);
            this.rptCustomerMarriage.DataBind();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            DataTable dataTable = new DataTable();
            int y = 0;
            for (int i = 0; i < area.Length; i++)
            {
                DataTable dt = ManageAnnounce.GetAnnounces(area[i].AreaId).Tables[0];
                if (y == 0)
                {
                    dataTable = dt.Clone();
                    y++;
                }
                foreach (DataRow tmpRow in dt.Rows)
                {
                    dataTable.ImportRow(tmpRow);
                }
            }
            this.RptAnnounce.DataSource = dataTable;
            this.RptAnnounce.DataBind();
        }

        protected string DecryptString(object obj)
        {
            DESEncrypt mydes = new DESEncrypt();
            return mydes.DecryptString(obj.ToString());
        }

        private ArrayList GetBirthData(Guid[] idAry)
        {
            ArrayList result = new ArrayList();
            ArrayList list = new ArrayList();
            ArrayList listNextYear = new ArrayList();
            for (int i = 0; i < idAry.Length; i++)
            {
                string strLevel = "";
                switch (ManageCustomer.GetCustomerBasicInfoViaId(idAry[i]).Level)
                {
                    case 1:
                        strLevel = "level1";
                        break;

                    case 2:
                        strLevel = "level2";
                        break;

                    case 3:
                        strLevel = "level3";
                        break;

                    case 4:
                        strLevel = "level4";
                        break;

                    case 5:
                        strLevel = "level5";
                        break;
                }
                int iDay = RemindCustomerInfo.GetLevelDayCount(strLevel);
                ArrayList tmpList = RemindCustomerInfo.CustomerBirthDate(idAry[i], iDay);
                list.AddRange(tmpList);
                ArrayList tmpList2 = RemindCustomerInfo.CustomerBirthDateInNextYear(idAry[i], iDay);
                listNextYear.AddRange(tmpList2);
            }
            result.AddRange(list);
            result.AddRange(listNextYear);
            return result;
        }

        private ArrayList GetMarriageData(Guid[] idAry)
        {
            ArrayList result = new ArrayList();
            ArrayList list = new ArrayList();
            ArrayList listNextYear = new ArrayList();
            for (int i = 0; i < idAry.Length; i++)
            {
                string strLevel = "";
                switch (ManageCustomer.GetCustomerBasicInfoViaId(idAry[i]).Level)
                {
                    case 1:
                        strLevel = "level1";
                        break;

                    case 2:
                        strLevel = "level2";
                        break;

                    case 3:
                        strLevel = "level3";
                        break;

                    case 4:
                        strLevel = "level4";
                        break;

                    case 5:
                        strLevel = "level5";
                        break;
                }
                int iDay = RemindCustomerInfo.GetLevelDayCount(strLevel);
                ArrayList tmpList = RemindCustomerInfo.CustomerMarriageDate(idAry[i], iDay);
                list.AddRange(tmpList);
                ArrayList tmpList2 = RemindCustomerInfo.CustomerMarriageDateInNextYear(idAry[i], iDay);
                listNextYear.AddRange(tmpList2);
            }
            result.AddRange(list);
            result.AddRange(listNextYear);
            return result;
        }

        private ArrayList GetMBirthData(Guid[] idAry)
        {
            ArrayList result = new ArrayList();
            ArrayList list = new ArrayList();
            ArrayList listNextYear = new ArrayList();
            for (int i = 0; i < idAry.Length; i++)
            {
                string strLevel = "";
                switch (ManageCustomer.GetCustomerBasicInfoViaId(idAry[i]).Level)
                {
                    case 1:
                        strLevel = "level1";
                        break;

                    case 2:
                        strLevel = "level2";
                        break;

                    case 3:
                        strLevel = "level3";
                        break;

                    case 4:
                        strLevel = "level4";
                        break;

                    case 5:
                        strLevel = "level5";
                        break;
                }
                int iDay = RemindCustomerInfo.GetLevelDayCount(strLevel);
                ArrayList tmpList = RemindCustomerInfo.FamilyMemberBirthDate(idAry[i], iDay);
                list.AddRange(tmpList);
                ArrayList tmpList2 = RemindCustomerInfo.FamilyMemberBirthDateInNextYear(idAry[i], iDay);
                listNextYear.AddRange(tmpList2);
            }
            result.AddRange(list);
            result.AddRange(listNextYear);
            return result;
        }

        protected string GetMsg4CustomerBirthDate(object obj1, object obj2, object obj3, object obj4, object obj5, object obj6)
        {
            string tmpStr1 = ManageCompany.GetCompanyViaId((Guid) obj1).CompanyName;
            string tmpStr2 = obj2.ToString();
            string tmpStr3 = obj3.ToString();
            string tmpStr4 = obj4.ToString();
            string tmpStr5 = obj5.ToString();
            string tmpStr6 = obj6.ToString();
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    return ("您的客户：" + tmpStr1 + " " + tmpStr2 + " " + tmpStr3 + " 将于" + tmpStr4.Substring(0, 2) + "月" + tmpStr4.Substring(3, 2) + "日过生日， 距离今天还有" + tmpStr5 + "天。");

                case "en":
                    return ("Today is " + tmpStr5 + " days left for the birthday " + tmpStr4 + " of your customer " + tmpStr3 + " (title " + tmpStr1 + " " + tmpStr2 + " )");
            }
            return "";
        }

        protected string GetMsg4CustomerMarriageDate(object obj1, object obj2, object obj3, object obj4, object obj5, object obj6)
        {
            string tmpStr1 = ManageCompany.GetCompanyViaId((Guid) obj1).CompanyName;
            string tmpStr2 = obj2.ToString();
            string tmpStr3 = obj3.ToString();
            string tmpStr4 = obj4.ToString();
            string tmpStr5 = obj5.ToString();
            string tmpStr6 = obj6.ToString();
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    return ("您的客户：" + tmpStr1 + " " + tmpStr2 + " " + tmpStr3 + " 将于" + tmpStr4.Substring(0, 2) + "月" + tmpStr4.Substring(3, 2) + " 发生结婚纪念日，距离今天还有" + tmpStr5 + "天。");

                case "en":
                    return ("Today is " + tmpStr5 + " days left for the wedding anniversary " + tmpStr4 + " of your customer " + tmpStr3 + " (title " + tmpStr1 + " " + tmpStr2 + " )");
            }
            return "";
        }

        protected string GetMsg4CustomerMBirthDate(object obj1, object obj2, object obj3, object obj4, object obj5, object obj6, object obj7, object obj8)
        {
            string tmpStr1 = ManageCompany.GetCompanyViaId((Guid) obj1).CompanyName;
            string tmpStr2 = obj2.ToString();
            string tmpStr3 = obj3.ToString();
            string tmpStr4 = obj4.ToString();
            string tmpStr5 = obj5.ToString();
            string tmpStr6 = obj6.ToString();
            string tmpStr7 = obj7.ToString();
            string tmpStr8 = obj8.ToString();
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    return ("您的客户：" + tmpStr1 + " " + tmpStr2 + " " + tmpStr3 + " 的" + tmpStr4 + " " + tmpStr5 + " 将于" + tmpStr6.Substring(0, 2) + "月" + tmpStr6.Substring(3, 2) + "过生日，距离今天还有 " + tmpStr7 + "天。");

                case "en":
                    return ("Today is " + tmpStr7 + " days left for the birthday " + tmpStr6 + " of " + tmpStr4 + " " + tmpStr5 + " of your customer " + tmpStr3 + " (title " + tmpStr1 + " " + tmpStr2 + " )");
            }
            return "";
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.lbUser.Text = this.Principal.OwnRole.RoleName + "/" + this.Principal.SysUser.LoginName;
                this.SetFrom();
                this.SetFromImage();
                if (this.Principal.OwnRole.TypeB == 0)
                {
                    this.DateBind();
                }
                else if (this.Principal.OwnRole.TypeB == 1)
                {
                    this.DateBind();
                }
                else if (this.Principal.OwnRole.TypeB == 2)
                {
                }
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbAnnounce.Text = ManageSystemConfig.GetSystemSetLanguage("MainPage", "lbAnnounce", language, path);
            this.lbCustomerBirth.Text = ManageSystemConfig.GetSystemSetLanguage("MainPage", "lbCustomerBirth", language, path);
            this.lbCustomerMarriage.Text = ManageSystemConfig.GetSystemSetLanguage("MainPage", "lbCustomerMarriage", language, path);
            this.lbCustomerFamilyMemberBirth.Text = ManageSystemConfig.GetSystemSetLanguage("MainPage", "lbCustomerFamilyMemberBirth", language, path);
            this.lbUserTitle.Text = ManageSystemConfig.GetSystemSetLanguage("MainPage", "lbUserTitle", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iAnnounce.ImageUrl = "images/title_AnnounceInfo.jpg";
                    break;

                case "en":
                    this.iAnnounce.ImageUrl = "images/title_AnnounceInfoEn.jpg";
                    break;
            }
        }
    }
}

